import requests
import pandas as pd
from lxml import etree
from datetime import datetime

# 中图网畅销榜Top1000

# https://www.bookschina.com/24hour/1_0_{i}/
# https://www.bookschina.com/24hour/1_0_2/
# https://www.bookschina.com/24hour/1_0_34/


if __name__ == '__main__':
    '''
    定义列表
    '''
    book_name_list = []
    book_author_list = []
    book_publisher_list = []
    star_num_list = []
    book_comments_list = []
    # 现价
    sell_price_list = []
    # 折扣
    # (3.2折)
    discount_list = []
    # 原价
    del_price_list = []
    # 其他优惠
    active_icon_list = []

    time_now = datetime.strftime(datetime.now(), '%Y-%m-%d %H-%M-%S')
    for page in range(1, 35):
        print(f"正在爬取中图网畅销榜Top1000第 {page} 页数据".center(50, '*'))
        ztw_url = f'https://www.bookschina.com/24hour/1_0_{page}/'

        ztw_headers = {
            'cookie': 'BookUser=1%7c59482653-427e-40c1-bcf0-7c337935cb56%7c1%7c0%7c638714281994641633%7c20180722%7ceec992ade8912a9c; UserSign=069f073dff21b10b; adtanchu=1; indexCache=yes; ASP.NET_SessionId=5tlvic5gc2yamhmrhfoxh4cl; Hm_lvt_6993f0ad5f90f4e1a0e6b2d471ca113a=1733210601,1733366002; HMACCOUNT=51CFE8B0D92C936E; Hm_lpvt_6993f0ad5f90f4e1a0e6b2d471ca113a=1733378818',
            'host': 'www.bookschina.com',
            'referer': ztw_url,
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
        }

        # 发送请求，获取数据
        ztw_response = requests.get(url=ztw_url, headers=ztw_headers)
        ztw_response.encoding = 'gb2312'
        ztw_html_str = ztw_response.text
        ztw_html = etree.HTML(ztw_html_str)

        # 通过xpath语法找到图书列表标签
        li_list = ztw_html.xpath('//div[@class="bookList"]/ul/li')

        # 遍历li的列表，得到每一个图书的li标签
        for li in li_list:
            # 书名
            book_name = str(li.xpath('./div[@class="infor"]/h2/a/text()')[0])
            book_name_list.append(book_name)

            # 作者
            book_author_a = li.xpath('./div[@class="infor"]/div[@class="author"]/a')[0]
            # 标签通过.attrib.get()根据属性名获取对应的属性值
            href_str = book_author_a.attrib.get('href')
            if 'book_find2' in href_str:
                book_author = book_author_a.xpath('string(.)')
            else:
                book_author = "无作者信息"
            book_author_list.append(book_author)

            # 出版社
            book_publisher_a = li.xpath('./div[@class="infor"]/div[@class="publisher"]/a')[0]
            # 标签通过.attrib.get()根据属性名获取对应的属性值
            href_str = book_publisher_a.attrib.get('href')
            if 'publish' in href_str:
                book_publisher = book_publisher_a.xpath('string(.)')
            else:
                book_publisher = "无出版社信息"
            book_publisher_list.append(book_publisher)

            startWrap_div = li.xpath('./div[@class="infor"]/div[@class="startWrap"]')[0]
            # 评分星级
            i_list = startWrap_div.xpath('./i')
            star_num = 0
            for i in i_list:
                star = i.attrib.get('class')
                if star=='one':
                    star_num+=1
                elif star=='half':
                    star_num+=0.5
                else:
                    star_num = 0
                    break
            star_num_list.append(star_num)

            # 评论数
            book_comments = str(startWrap_div.xpath('./a/text()')[0])
            book_comments_list.append(book_comments)

            # 现价
            sell_price = str(
                li.xpath('./div[@class="infor"]/div[@class="priceWrap"]/span[@class="sellPrice"]/text()')[0])[1:]
            sell_price_list.append(sell_price)

            # 折扣
            # (3.2折)
            discount = str(li.xpath('./div[@class="infor"]/div[@class="priceWrap"]/span[@class="discount"]/text()')[0])[
                       1:-1]
            discount_list.append(discount)

            # 原价
            del_price = str(li.xpath('./div[@class="infor"]/div[@class="priceWrap"]/del/text()')[0])[1:]
            del_price_list.append(del_price)

            # 其他优惠
            try:
                active_icon = str(li.xpath('./div[@class="infor"]/div[@class="activeIcon"]/a/text()')[0])
            except:
                active_icon = "无其他优惠"
            active_icon_list.append(active_icon)

            print(
                f"书名:{book_name}, 作者:{book_author}, 出版社:{book_publisher}, 评论星级:{star_num}颗星, 评论数:{book_comments}, 现价:{sell_price}, 折扣:{discount}, 原价:{del_price}, 其他优惠:{active_icon}")
            print("-----------------------")

    # 将每个列表封装到字典中
    books_dict = {
        '书名': book_name_list,
        '作者': book_author_list,
        '出版社': book_publisher_list,
        '评论星级': star_num_list,
        '评论数': book_comments_list,
        '现价': sell_price_list,
        '折扣': discount_list,
        '原价': del_price_list,
        '其他优惠': active_icon_list
    }

    df1 = pd.DataFrame(books_dict)
    df1.to_csv(f'data/中图网畅销书籍前1000-{time_now}.csv', index=False)





